<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="http://localhost/3.34/3.34/esri/css/esri.css">
    <script src="http://localhost/3.34/3.34/init.js"></script>
    <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
    <style>
        html,
        body,
        #viewDiv {
            height: 100%;
            width: 100%;
            margin: 0;
            padding: 0;
        }
    </style>
    <script>
        require([
            "esri/map",
            "esri/SpatialReference",
            "esri/layers/ArcGISDynamicMapServiceLayer",
            "esri/layers/ArcGISTiledMapServiceLayer",
            "esri/layers/FeatureLayer",
            "esri/layers/MapImageLayer",
            "esri/toolbars/navigation",
            "esri/toolbars/draw",
            "esri/geometry/Circle",
            "esri/units",

            "esri/graphic",
            "esri/geometry/Extent",
            "esri/geometry/geodesicUtils",
            "esri/geometry/webMercatorUtils",

            "esri/tasks/GeometryService",
            "esri/tasks/AreasAndLengthsParameters",
            "esri/tasks/LengthsParameters",
            "esri/tasks/geometry",

            "esri/tasks/PrintTask",
            "esri/tasks/PrintTemplate",
            "esri/tasks/PrintParameters",

            "esri/symbols/LineSymbol",
            "esri/symbols/SimpleLineSymbol",
            "esri/symbols/SimpleFillSymbol",
            "esri/symbols/PictureMarkerSymbol",
            "esri/symbols/MarkerSymbol",
            "esri/symbols/SimpleMarkerSymbol",
            "esri/symbols/TextSymbol",
            "esri/symbols/Font",

            "esri/dijit/editing/Add",
            "esri/dijit/editing/Delete",
            "esri/dijit/editing/Update",

            "esri/InfoTemplate",

            "esri/tasks/QueryTask",
            "esri/tasks/query",
            "esri/tasks/IdentifyTask",
            "esri/tasks/IdentifyParameters",
            "esri/tasks/FindTask",
            "esri/tasks/FindParameters",
            "esri/tasks/FindResult",
            "esri/tasks/Geoprocessor",
            "esri/tasks/FeatureSet",

            "esri/geometry/geometryEngine",
            "esri/geometry/Point",
            "esri/Color",

            "dojo/dom",
            "dojo/on",
            "dojo/domReady!"
        ], function(
            Map,
            SpatialReference,
            ArcGISDynamicMapServiceLayer,
            ArcGISTiledMapServiceLayer,
            FeatureLayer,
            MapImageLayer,

            Navigation,
            Draw,
            Circle,
            Units,

            Graphic,
            Extent,
            geodesicUtils,
            webMercatorUtils,

            GeometryService,
            AreasAndLengthsParameters,
            LengthsParameters,
            Geometry,

            PrintTask,
            PrintTemplate,
            PrintParameters,

            LineSymbol,
            SimpleLineSymbol,
            SimpleFillSymbol,
            PictureMarkerSymbol,
            MarkerSymbol,
            SimpleMarkerSymbol,
            TextSymbol,
            Font,

            Add,
            Delete,
            Update,

            InfoTemplate,

            QueryTask,
            Query,
            IdentifyTask,
            IdentifyParameters,
            FindTask,
            FindParameters,
            FindResult,
            Geoprocessor,
            FeatureSet,

            geometryEngine,
            Point,
            Color,


            dom,
            on
        ) {
            var startExtent = new esri.geometry.Extent(394447.65239553, 2813852.1189, 491999.92010447, 2862729.8697, new esri.SpatialReference(4549));
            //创建地图
            var map = Map("viewDiv", {
                //extent: startExtent,
                basemap: "topo",
                center: [120.395907, 36.112175],
                zoom: 11,
                nav: false,
                slider: false,
                isDoubleClickZoom: false,
                logo: false
            });
            // var ImageMapLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://192.168.0.106:6080/arcgis/rest/services/qingdao/MapServer");
            // map.addLayer(ImageMapLayer);

            // var graphic;
            // var features = [];
            // var button = document.getElementById("btn");
            // button.onclick = function() {
            //     map.on("click", function(event) {
            //         var textSymbol = new TextSymbol(event.mapPoint.x);

            //         var color = new Color("#ff0000");
            //         textSymbol.setColor(color);

            //         var font = new Font();
            //         font.setSize("12pt");
            //         //font.setFamily("Arial");

            //         //textSymbol.setFont(font);

            //         var point = new Point(
            //             event.mapPoint.x,
            //             event.mapPoint.y,
            //             new SpatialReference({
            //                 wkid: map.SpatialReference
            //             })
            //         );
            //         graphic = new Graphic(point, textSymbol);
            //         features.push(graphic);
            //         map.graphics.add(graphic);
            //     });
            // };
            var features;
            $("#btn").on("click", function() {
                var draw = new esri.toolbars.Draw(map, {
                    showTooltips: true
                });
                draw.activate(esri.toolbars.Draw.POLYGON);
                dojo.connect(draw, "onDrawEnd", addToMap);

                function addToMap(geo) {
                    console.log(geo);
                    draw.deactivate();
                    map.showZoomSlider();
                    var symbol = new esri.symbol.SimpleFillSymbol(
                        esri.symbol.SimpleFillSymbol.STYLE_SOLID,
                        new esri.symbol.SimpleLineSymbol(
                            esri.symbol.SimpleLineSymbol.STYLE_DASHDOT,
                            new dojo.Color([255, 0, 0]), 2),
                        new dojo.Color([255, 255, 0, 0.25])
                    );
                    features = new esri.Graphic(geo, symbol);
                    map.graphics.add(features);
                }
            });

            var buttonConvert = document.getElementById("btnConvert");
            buttonConvert.onclick = function() {
                var gp = new Geoprocessor("http://localhost:6080/arcgis/rest/services/gp/featureSetToCAD/GPServer/featureSetToCAD");

                // var graphic = new Graphic(...);
                // var features = [];
                // features.push(graphic);
                // var featureSet = new FeatureSet();
                // featureSet.features = features;

                // var feature = new Feature({
                //     graphic: features,
                //     map: map,
                //     spatialReference: spatialReference
                // });

                var in_featureSet = new FeatureSet();
                //in_featureSet.fields = ;
                //in_featureSet.features.push(features);
                in_featureSet.features = features;

                //输出类型
                var Output_Type = "DWG_R2010";
                // var Output_Type = "C:\Users\yws\Desktop\cad\test.dwg";
                var Output_File = "C:\Users\yws\Desktop\cad";

                var params = {
                    Output_Type: Output_Type,
                    InPut_FeatureSet: [in_featureSet],
                    //Output_File: Output_File,
                    //currentTime: Output_File,
                    //Output_Type: InputFeatureSet
                };
                gp.submitJob(params, Result);

                function Result(res) {
                    console.log(res);
                    if (res.jobStatus = "esriJobSucceeded") {
                        alert("已生成");
                    } else {
                        alert("执行工具失败");
                    }
                }
                //gp.submitJob(params, completeCallback, statusCallback);

                // function statusCallback(jobInfo) {
                //     console.log(jobInfo.jobStatus);
                // }

                // function completeCallback(jobInfo) {
                //     var imageParams = new ImageParameters();
                //     imageParams.imageSpatialReference = map.spatialReference;
                //     gp.getResultImageLayer(jobInfo.jobId, "Selected_Tax_Lot", imageParams, function(gpLayer) {
                //         gpLayer.setOpacity(0.5);
                //         map.addLayer(gpLayer);
                //     });
                // }

                // function Results(res) {
                //     console.log(res);
                //     //if(res)
                // }
            };
        });
    </script>
</head>

<body>
    <div id="viewDiv">
        <button id="btn">点击</button>
        <button id="btnConvert">生成cad</button>
    </div>
</body>

</html>